package com.tencent.wxcloudrun.model.support;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author zhe.wang@weimob.com
 * @date 2022/4/28 下午4:44
 */
@Data
@NoArgsConstructor
public class R<T> {
    /**
     * 状态码
     */
    private Integer code;

    /**
     * 返回消息
     */
    private String message;


    /**
     * 返回数据
     */
    private T data;

    public R(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public R(IResultCode resultCode, T data) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.data = data;
    }

    public static R success() {
        return new R(ResultCode.OK, true);
    }

    public static <T> R<T> success(T data) {
        return new R<>(ResultCode.OK, data);
    }

    public static R fail() {
        return new R(ResultCode.ERROR, false);
    }

    public static R fail(String message) {
        return new R(-1, message, false);
    }
}
